package ajax.quanxuan.sys.service.impl;

import ajax.quanxuan.sys.entity.Dept;
import ajax.quanxuan.sys.entity.Permission;
import ajax.quanxuan.sys.dao.PermissionMapper;
import ajax.quanxuan.sys.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KazuGin
 * @since 2024-07-04
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
   @Resource
   private PermissionMapper permissionMapper;
    @Override
    public boolean removeById(Serializable id){
        //删除第三张关系表的数据(sys_role_permission)
        //根据菜单id或权限id删除sys_role_permission权限菜单关系表数据
        permissionMapper.deleteRolePermissionByPid(id);
        return super.removeById(id);
    }

    @Override
    public List<Integer> findRolePermissionByRoleId(int roleId) {
        return permissionMapper.findRolePermissionByRoleId(roleId);
    }

    @Override
    public List<Permission> list(Permission permission) {
        return permissionMapper.list(permission);
    }

    @Override
    public List<Permission> listAll(Permission permission) {
        return permissionMapper.listAll(permission);
    }

    @Override
    public List<Permission> listMun(Permission permission) {
     if (permission.getPage()<0){
            permission.setPage(1);
        }
        //获取最大数量
        Integer count =Integer.valueOf(permissionMapper.countmenu(permission));
        //获取总页数   count/rows
        Integer size = count / permission.getLimit();
        if (count % permission.getLimit() > 0) {
            size++;
        }
        permission.setSize(size);
        //如果当前页数大于总页数   则当前页等于总页数
        if (size == 0) {
            permission.setPage(1);
        } else if (permission.getPage() > permission.getSize()) {
            permission.setPage(size);
        }
        permission.setCurrIndex((permission.getPage() - 1) * permission.getLimit());
        permission.setPageSize(permission.getLimit());

        return permissionMapper.listMun(permission);
    }

    @Override
    public List<Permission> listMunleft(Permission permission) {
        return permissionMapper.listMunleft(permission);
    }

    @Override
    public List<Permission> findById(String pidlist) {
        return permissionMapper.findById(pidlist);
    }

    @Override
    public List<Permission> listrolemun(String pidlist) {
        return permissionMapper.listrolemun(pidlist);
    }

    @Override
    public Integer countmenu(Permission permission) {
        return permissionMapper.countmenu(permission);
    }

    @Override
    public List<Permission> listPermission(Permission permission) {
        if (permission.getPage()<0){
            permission.setPage(1);
        }
        //获取最大数量
        Integer count =Integer.valueOf(permissionMapper.countPermission(permission));
        //获取总页数   count/rows
        Integer size = count / permission.getLimit();
        if (count % permission.getLimit() > 0) {
            size++;
        }
        permission.setSize(size);
        //如果当前页数大于总页数   则当前页等于总页数
        if (size == 0) {
            permission.setPage(1);
        } else if (permission.getPage() > permission.getSize()) {
            permission.setPage(size);
        }
        permission.setCurrIndex((permission.getPage() - 1) * permission.getLimit());
        permission.setPageSize(permission.getLimit());
        return  permissionMapper.listPermission(permission);
    }

    @Override
    public Integer countPermission(Permission permission) {
        return permissionMapper.countPermission(permission);
    }

    @Override
    public List<Permission> listPermissiontrerr(Permission permission) {
        return permissionMapper.listPermissiontrerr(permission);
    }
}
